//
//  SYShowNewsController.m
//  MicroNews
//
//  Created by tongcheng on 15/10/20.
//  Copyright © 2015年 siyue. All rights reserved.
//

#import "SYShowNewsController.h"
#import "NewsCell.h"
#import "NewsModel.h"
#import "SYNewsDetailViewController.h"


@interface SYShowNewsController()

//@property (strong,nonatomic)NSMutableArray *newsArray;

@property (nonatomic, strong) NSArray *newsList;

//@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation SYShowNewsController

- (void)setNewsList:(NSArray *)newsList {
    
    _newsList = newsList;
    [self.tableView reloadData];
}

- (void)setUrlString:(NSString *)urlString {
    _urlString = urlString;
    
    // 清空缓存
    self.newsList = nil;
    
    __weak typeof(self) weakSelf = self;
    
    NSString *urlStr = [NSString stringWithFormat:@"%@&view=top&p=1",WDIndex];
//    NSLog(@"--><#name#>--------》%@",urlStr);
    [NewsModel loadDataWithURLString:urlStr finished:^(NSArray *newsList) {
        weakSelf.newsList = newsList;
    }];
    
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = SYColor(239, 239, 244);
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;

    __weak UITableView *tableView = self.tableView;
    
    tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//         [self loadData];
        [tableView reloadData];
        [tableView.header endRefreshing];
    }];
    
    tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [tableView.footer endRefreshing];
    }];

}


-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.newsList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    //    声明静态字符串型对象，用来标记重用单元格
//    static NSString *TableSampleIdentifier = @"NewsCell";
//    //    用TableSampleIdentifier表示需要重用的单元
//    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    
    // 1. 创建cell
    NewsCell *cell = [NewsCell cellWithTable:tableView];
    
    //通过数据模型 设置Cell内容
    cell.news = self.newsList[indexPath.row];
    
    cell.source.text = self.title;

    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    NewsModel *model = self.newsList[row];
    NSString *newsId = model.iid;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NewsStoryboard" bundle:nil];
    SYNewsDetailViewController *newsDetailVC = [storyboard instantiateViewControllerWithIdentifier:@"SYNewsDetailViewController"];
    newsDetailVC.newsId = newsId;
    [self.navigationController pushViewController:newsDetailVC animated:YES];
}

#pragma mark - 删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [self.Array removeObjectAtIndex:indexPath.row];
//    
//    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

@end
